import Vue from 'vue'
import axios from 'axios'
import ElementUI from "element-ui";
import 'element-ui/lib/theme-chalk/index.css';
import baseUtil from "@/utils/baseUtil";
import globalConfig from "@/config/global_config";
import formatUtil from "@/utils/formatUtil";
import "./assets/style/style.css";
import "./assets/iconfont/iconfont.css";
import SimpleVueValidation from "simple-vue-validator";
import validatorMessage from "@/api/validatorMessage";
import App from './App'
import router from './router'
import store from './store'
import md5 from 'js-md5';

if (!process.env.IS_WEB) Vue.use(require('vue-electron'))
SimpleVueValidation.extendTemplates(validatorMessage);
Vue.http = Vue.prototype.$http = axios
Vue.config.productionTip = false
Vue.prototype.baseUtil = baseUtil;
Vue.prototype.fmUtil = formatUtil;
Vue.prototype.baseUrl = globalConfig.base_url;
Vue.prototype.baseWebUrl = globalConfig.base_web_url;
Vue.use(SimpleVueValidation);
Vue.use(ElementUI);
Vue.prototype.validator = SimpleVueValidation.Validator;
Vue.prototype.$md5 = md5;


router.beforeEach((to, from, next) => {
  if(to.path.indexOf('/login') == -1 && (baseUtil.isBlank(store.getters.user) || !store.getters.loginStatus)){
    store.dispatch("LogOutAction")
    next({path: '/login'})
  }else {
    next()
  }
})

// 页面刷新时，重新赋值user
if (window.localStorage.getItem('user')) {
  let userInfo = JSON.parse(window.localStorage.getItem('user'))
  let currentMenu = JSON.parse(window.localStorage.getItem('currentMenu'))
  store.dispatch('LoginAction',userInfo);
  store.dispatch('PushCurrentMenu',currentMenu);
}

/* eslint-disable no-new */
new Vue({
  components: { App },
  router,
  store,
  template: '<App/>'
}).$mount('#app')
